package edu.msg.hrappy.web.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;

import edu.msg.hrappy.backend.model.Candidate;
import edu.msg.hrappy.backend.model.Comment;
import edu.msg.hrappy.backend.model.Job;
import edu.msg.hrappy.backend.model.User;
import edu.msg.hrappy.backend.service.CandidateService;
import edu.msg.hrappy.backend.service.CommentService;
import edu.msg.hrappy.backend.service.JobService;
import edu.msg.hrappy.backend.service.UserService;

/**
 * Servlet implementation class AddCandidateServlet
 */

public class EditCommentToJobServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@EJB
	private CandidateService candidateService;
	@EJB
	private UserService userService;
	@EJB
	private CommentService commentService;
	@EJB
	JobService jobService;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EditCommentToJobServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = userService.getUserByUserName(request.getUserPrincipal()
				.toString());
		session.setAttribute("userId", user.getId());
		session.setAttribute("username", user.getUserName());

		Job job=jobService.getById(Long.parseLong(request.getParameter("jobId")));
		session.setAttribute("job", job);
		String address = "./OnePozition";
		RequestDispatcher dispatcher = request.getRequestDispatcher(address);
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Date date = new Date();
		String commentText = "";
		Comment comment = null;
		Job job = null;

		commentText = (String) request.getParameter("comment");

		String jobId = (String) request.getParameter("jobId");
		job = jobService.getById(Long.parseLong(jobId));

		String commentId = (String) request.getParameter("comId");
		comment = commentService.getById(Long.parseLong(commentId));

	
		comment.setDate(date);
		comment.setText(commentText);

		commentService.update(comment);
		jobService.update(job);
		HttpSession session = request.getSession();
		Job job2=jobService.getById(Long.parseLong(request.getParameter("jobId")));
		session.setAttribute("job", job2);
		User user = userService.getUserByUserName(request.getUserPrincipal()
				.toString());
		session.setAttribute("userId", user.getId());
		session.setAttribute("username", user.getUserName());
		String address2 = "./OnePozition";
		RequestDispatcher dispatcher = request.getRequestDispatcher(address2);
		dispatcher.forward(request, response);

	}

}
